<?php

class admin_app_menu extends admin_app
{
	
	public function __construct()
	{
		$my = bone::get_admin();
		if($my->guest)$this->redirect('./?app=user&task=login', '登陆超时', 'error');
		
		parent::__contruct( 'menu', '菜单管理' );
	}
	

	//菜单组列表
	public function group_listing()
	{
		$template = $this->get_template('group_listing');
		$template->set_title('菜单组');
		$template->set_data($this->get_data());
		$template->display();
	}

	// 添加/编辑菜单组
	public function group_edit()
	{
		$group_id = get('group_id', 0, 'int');

		$group = $this->get_table('menu_group');
		$group->load($group_id);

		$template = $this->get_template('group_edit');
		if($group_id)
			$template->set_title('编辑菜单组');
		else
			$template->set_title('新建菜单组');
		$template->set('group', $group);
		$template->set_data($this->get_data());
		$template->display();
	}

	// 保存菜单组
	public function group_edit_save()
	{
		$group = $this->get_table('menu_group');
		$group->bind( $_POST );
		$group->save();

		$model = $this->get_model('menu');
		$model->update_menu_file($group->id);

		$this->redirect('./?app=menu&task=group_listing', '保存成功');
	}
	

	// 删除菜单组
	public function group_delete()
	{
		$model = $this->get_model('menu');
		$model->delete_group( get('group_id', 0, 'int') );

		$this->redirect('./?app=menu&task=group_listing', '删除成功');
	}


	// 菜单项列表
	public function listing()
	{
		$group_id = get('group_id', 0, 'int');
		if(!$group_id)
		{
			$group_list = $this->get_data('group_list');
			if(count($group_list))
				$group_id = $group_list[0]->id;
		}

		$group = $this->get_table('menu_group');
		$group->load($group_id);

		$model = $this->get_model('menu');
		$menu_list = $model->get_menu_tree_list( $group_id );

		$template = $this->get_template('listing');
		$template->set_title($group->name);
		$template->set('group', $group);
		$template->set('menu_list', $menu_list);
		$template->set_data($this->get_data());
		$template->display();
	}



	public function listing_save()
	{
		$group_id = post('group_id', 0, 'int');

		$ids = post('id', 0, 'array(int)');
		$parent_ids = post('parent_id', 0, 'array(int)');
		$names = post('name', 0, 'array(string)');

		$controllers = post('m_controller', '');
		$tasks = post('m_task', '');
		$params = post('param', '');
		$urls = post('url', '');
		$link_tos = post('link_to', '');
		
		if($ids)
		{
			if(is_array($ids))
			{
				for($i=0,$n=count($ids); $i<$n; $i++ )
				{
					if( !($ids[$i] || $names[$i]) ) continue;

					$table = $this->get_table('menu');
					$table->id = $ids[$i];
					$table->group_id = $group_id;
					$table->parent_id = $parent_ids[$i];
					$table->name = $names[$i];
					$table->controller = $controllers[$i];
					$table->task = $tasks[$i];
					$table->param = $params[$i];
					$table->url = $urls[$i];
					$table->link_to = $link_tos[$i];
					$table->ordering = $i;
					$table->save();
				}
			}
		}

		$model = $this->get_model('menu');
		$model->update_menu_file($group_id);

		$this->redirect('./?app=menu&task=listing&group_id='.$group_id, '保存菜单成功');
	}

	// 删除菜单项
	public function ajax_delete_menu()
	{
		$menu_id = get('menu_id', 0, 'int');
		if( ! $menu_id)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{menu_id}丢失');
		}
		else
		{
			$menu = bone::get_table('menu');
			$menu->load($menu_id);
			$model = $this->get_model('menu');
			if( ! $model->delete_menu( $menu_id ) )
			{
				$this->set_ajax('status', 2);
				$this->set_ajax('description', $model->get_error());
			}
			$model->update_menu_file($menu->group_id);
		}

		$this->set_ajax('status', 0);
		$this->set_ajax('description', '删除成功');
		$this->ajax();
	}


	public function ajax_link()
	{
		$menu_id = get('menu_id', 0, 'int');
		$is_backend = get('is_backend', 0, 'int');

		if(!$menu_id) exit('参数{menu_id}丢失');

		$model = $this->get_model('application');
		$application_list = $model->get_list();

		if( $is_backend==1 )
			$controller_list = $model->get_backend_controller_list();
		else
			$controller_list = $model->get_frontend_controller_list();


		$template = $this->get_template('ajax_link');
		$template->set_title('连接到应用');
		$template->set('menu_id', $menu_id);
		$template->set('application_list', $application_list);
		$template->set('controller_list', $controller_list);
		$template->set_data($this->get_data());
		$template->display();

		exit;
	}



	

}

